package com.dormitoryAdministratorSystem.ui.studentmanage;

import com.dormitoryAdministratorSystem.entity.student.StudentBean;
import com.dormitoryAdministratorSystem.service.student.impl.StudentSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.dormitoryAdministratorSystem.util.BeansUtil;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoLabel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Set;

public class StudentChgDialog extends JDialog {
	
	private LovoLabel nameLab;
	private LovoLabel genderLab;
	private LovoLabel classLab;
	private LovoLabel oldRoomLab;
	private LovoComboBox<String> newRoomCmb;//后期换做房间Bean
	
	private LovoButton chgBtn;
	private LovoButton cancelBtn;

	private int id;
	private StudentSerImpl ssi = new StudentSerImpl();
	
	
	public StudentChgDialog(MainFrame frame, int id){
		super(frame, "学生换房", true);
		this.setBounds(450, 350, 450, 280);
		this.setLayout(null);
		this.id = id;
		this.init();

		this.setVisible(true);
	}

	private void init() {
		// TODO Auto-generated method stub
		this.nameLab = new LovoLabel("姓名",  20, 20, this);
		this.genderLab = new LovoLabel("性别", 220, 20, this);
		this.classLab = new LovoLabel("班级",  20, 80, this);
		this.oldRoomLab = new LovoLabel("原房间", 220, 80, this);
		StudentBean s = ssi.showStuInfo(id);
		nameLab.setText(s.getSName());
		genderLab.setText( s.getGender());
		classLab.setText(s.getClassBean().getCName());
		oldRoomLab.setText(s.getRoomBean().getAddress());
		
		ArrayList<String> roomLst = new ArrayList<String>();
		roomLst.add(s.getRoomBean().getAddress());
		Set<String> strings = BeansUtil.getrMap().keySet();
		for (String string : strings) {
			roomLst.add(string + "(" + BeansUtil.getrMap().get(string).getRoomType().substring(0,1) + ")");
		}

		this.newRoomCmb = new LovoComboBox<String>("房间",roomLst,  20, 140, this);
		
		
		this.chgBtn = new LovoButton("确定", 120, 200, this);
		this.cancelBtn = new LovoButton("取消", 270, 200, this);
		this.chgBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(newRoomCmb.getItem().contains("(")){
					if(!s.getGender().equals(
							newRoomCmb.getItem().substring(newRoomCmb.getItem().indexOf("(")+1,
									newRoomCmb.getItem().indexOf(")") ))){
						JOptionPane.showMessageDialog(null,"换房失败" +
								"\n男生只能换到男生寝室" +
								"\n女生只能换到女生寝室");
					}else {
						int isSure = JOptionPane.showConfirmDialog(null, "确定要换房吗");
						if(isSure == 0){
							int oldId = s.getRoomBean().getRId();
							s.setRoomBean(BeansUtil.getrMap()
									.get(newRoomCmb.getItem().substring(0,newRoomCmb.getItem().indexOf("("))));
							ssi.changeRoom(s, oldId);
							JOptionPane.showMessageDialog(null, "换房成功");
							StudentChgDialog.this.dispose();
						}
					}
				}
			}
		});

		this.cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				StudentChgDialog.this.dispose();
			}
		});
		
	}
}
